package dkbta.server.domain.cu;

import dkbta.server.gui.Observable;
import java.net.Socket;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import dkbta.server.gui.Observer;
import dkbta.server.util.query.QueriesTable;



public class CUStubsTable implements Observable{

	private Hashtable<String, CUStub> m_stubs;
	private Observer m_cuPanelObserver;
	private Observer m_logsPanelObserver;
        private Observer m_queriesPanelObserver;

	public CUStubsTable(){
		m_stubs = new Hashtable<String, CUStub>();
	}

	public synchronized void addStub(Socket cuStubSocket, QueriesTable queriesTable) {
		CUStub stub;		
		stub = new CUStub(cuStubSocket,queriesTable);				
		stub.addLogsPanelObserver(m_logsPanelObserver);
                stub.addQueriesPanelObserver(m_queriesPanelObserver);
                stub.addCUPanelObserver(m_cuPanelObserver);
		m_stubs.put(stub.getCUId(), stub);
                stub.start();
	}

	public synchronized CUStub get(String stubId){
		return m_stubs.get(stubId);
	}

	public synchronized Vector<String> getAvailableStubs() {
		Vector<String> ids = new Vector<String>();
		Collection<CUStub> stubs = m_stubs.values();
		Iterator<CUStub> itr = stubs.iterator();
		CUStub stub;
		while (itr.hasNext()) {
			stub = itr.next();
			if(stub.isAvailable()){
				ids.add(stub.getCUId());
			}
		}
		return ids;
	}

	public void addCUPanelObserver(Observer o) {
		m_cuPanelObserver = o;
	}

	public void addLogsPanelObserver(Observer o) {
		m_logsPanelObserver = o;
	}

    public void addQueriesPanelObserver(Observer o) {
        m_queriesPanelObserver = o;
    }

}
